#include "widgetbackgrounddrawer.h"
#include "toptabbar.h"
#include <QPainter>
#include <QRect>

using namespace ExecTerminalImplNS;

WidgetBackgroundDrawer::WidgetBackgroundDrawer(QWidget * parent):
theParentTabBar(0),
thePaintState(PS_NORMAL)
{
    theParentTabBar = qobject_cast<TopTabBar*>(parent);
}

WidgetBackgroundDrawer::PaintState WidgetBackgroundDrawer::backgroundPaintState()const
{
    return thePaintState;
}

void WidgetBackgroundDrawer::setBackgroundPaintState(PaintState st)
{
    thePaintState = st;
}

bool WidgetBackgroundDrawer::isTopLocation()const
{
    return theParentTabBar == 0 || theParentTabBar->orientation() == UP_ORIENTATION;
}

void WidgetBackgroundDrawer::drawBackground(QPainter * painter, QRect rect)
{
    painter->save();
    const int orientation = theParentTabBar ? theParentTabBar->orientation() : UP_ORIENTATION;
    if (orientation == DOWN_ORIENTATION)
    {
        rect.translate(0, -1);
    }
    QRect r2(rect);
    int x1 = r2.left();
    int x2 = r2.right();
    int y1 = r2.top();
    int y2 = r2.bottom();

    QMatrix rotMatrix;
    bool flip = false;
    switch (orientation) {
            case UP_ORIENTATION:
                break;
            case DOWN_ORIENTATION:
                rotMatrix.rotate(180);
                rotMatrix.translate(0, -rect.height() + 1);
                rotMatrix.scale(-1, 1);
                painter->setMatrix(rotMatrix);
                break;
            default:
                painter->restore();
                return;
    }

    if (flip) {
        QRect tmp = rect;
        rect = QRect(tmp.y(), tmp.x(), tmp.height(), tmp.width());
        int temp = x1;
        x1 = y1;
        y1 = temp;
        temp = x2;
        x2 = y2;
        y2 = temp;
    }
    const int width = x2 - x1 + 1;
    int height = y2 - y1 + 1;
    if (thePaintState != PS_SELECTED)
    {
        height -= 2;
    }

    QPixmap leftSide;
    QPixmap center;
    QPixmap rightSide;
    switch(thePaintState)
    {
    case PS_NORMAL:
    case PS_DISABLED:
        {
            static QPixmap ls(":/Resources/top_bookmarks_left.png");
            static QPixmap c(":/Resources/top_bookmarks_center.png");
            static QPixmap rs(":/Resources/top_bookmarks_right.png");
            leftSide = ls;
            center = c;
            rightSide = rs;
        }
    	break;
    case PS_HOVER:
        {
            static QPixmap ls(":/Resources/top_bookmarks_left_hover.png");
            static QPixmap c(":/Resources/top_bookmarks_center_hover.png");
            static QPixmap rs(":/Resources/top_bookmarks_right_hover.png");
            leftSide = ls;
            center = c;
            rightSide = rs;
        }
        break;
    case PS_SELECTED:
        {
            static QPixmap ls(":/Resources/top_bookmarks_active_left.png");
            static QPixmap c(":/Resources/top_bookmarks_active_center.png");
            static QPixmap rs(":/Resources/top_bookmarks_active_right.png");
            leftSide = ls;
            center = c;
            rightSide = rs;
        }
        break;
    case PS_PRESSED:
        {
            static QPixmap ls(":/Resources/top_bookmarks_left_down.png");
            static QPixmap c(":/Resources/top_bookmarks_center_down.png");
            static QPixmap rs(":/Resources/top_bookmarks_right_down.png");
            leftSide = ls;
            center = c;
            rightSide = rs;
        }
        break;
    default:
        break;
    }
    const int leftWidth = leftSide.width();
    const int rightWidth = rightSide.width();
    painter->drawPixmap(QRect(QPoint(x1, y1), leftSide.size()), leftSide, leftSide.rect());
    painter->drawTiledPixmap(x1 + leftWidth, y1, width - leftWidth -  rightWidth, height, center);
    painter->drawPixmap(QRect(QPoint(x2 - rightWidth + 1, y1), rightSide.size()), rightSide, rightSide.rect());
    painter->restore();

}

TopTabBar * WidgetBackgroundDrawer::parentTabBar()const
{
    return theParentTabBar;
}

